// ChangeOp.cpp : implementation file
//

#include "stdafx.h"
#include "VolDia.h"
#include "ChangeOp.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CVolDiaApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CChangeOp dialog


CChangeOp::CChangeOp(CWnd* pParent /*=NULL*/)
	: CDialog(CChangeOp::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChangeOp)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CChangeOp::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChangeOp)
	DDX_Control(pDX, IDC_CMBNEXT, m_cmbNextOp);
	DDX_Control(pDX, IDC_CMBCURR, m_cmbCurrOp);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChangeOp, CDialog)
	//{{AFX_MSG_MAP(CChangeOp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChangeOp message handlers

BOOL CChangeOp::OnInitDialog() 
{
	CDialog::OnInitDialog();
	RETCOND		retCnd;
	RETSTAT		retSt;
	FILEIDX		filePhas;
	phas_struc	phas;
	int			rec;
	int			currOp;
	int			nextOp;
	TCHAR		tempStr[10];

	filePhas.num = F_PHAS;
	
	this->m_cmbCurrOp.ResetContent();
	this->m_cmbNextOp.ResetContent();

	this->m_cmbCurrOp.AddString(_T("0"));
	for(rec = 1; rec <= N_MAX_PHASE; rec++){
		retCnd = FileRead(&filePhas, rec, 1, &phas, sizeof(phas));
		if(retCnd != R_NORMAL){
			MessageBox(_T("Error reading DAFS file F.PHAS."), _T("Error"), MB_OK);
			return FALSE;
		}
		if(phas.phase != 0){
			_stprintf_s(tempStr, _T("%d"), phas.phase);
			this->m_cmbCurrOp.AddString(tempStr);
			this->m_cmbNextOp.AddString(tempStr);
		}
		else
			break;
	}

	retSt = GetDynCurrentPhase(&currOp);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Error retrieving current operation."), _T("Error"), MB_OK);
		return FALSE;
	}
	
	retSt = GetDynNextPhase(&nextOp);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Error retrieving next operation."), _T("Error"), MB_OK);
		return FALSE;
	}

	_stprintf_s(tempStr, _T("%d"), currOp);
	rec = this->m_cmbCurrOp.FindStringExact(0, tempStr);
	if(rec != CB_ERR)
		this->m_cmbCurrOp.SetCurSel(rec);
	else
		this->m_cmbCurrOp.SetCurSel(0);

	_stprintf_s(tempStr, _T("%d"), nextOp);
	rec = this->m_cmbNextOp.FindStringExact(0, tempStr);
	if(rec != CB_ERR)
		this->m_cmbNextOp.SetCurSel(rec);
	else
		this->m_cmbNextOp.SetCurSel(0);


	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CChangeOp::OnOK() 
{
	RETSTAT		retSt;
	int			opNum;
	TCHAR		str[10];
	TCHAR		msg[128];
	int			currOp;
	int			nextOp;

	retSt = GetDynCurrentPhase(&currOp);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Error retrieving current operation."), _T("Error"), MB_OK);
		return;
	}
	
	retSt = GetDynNextPhase(&nextOp);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Error retrieving next operation."), _T("Error"), MB_OK);
		return;
	}

	this->m_cmbCurrOp.GetLBText(this->m_cmbCurrOp.GetCurSel(), str);
	opNum = _ttoi(str);
	if(opNum != currOp){
		retSt = SetDynCurrentPhase(opNum);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error setting current operation."), _T("Error"), MB_OK);
			theApp.m_pDlg->m_pPhaseExecDialog->EchoStrAdd(_T("Error changing current operation."));
			return;
		}
		else{
			_stprintf_s(msg, _T("Current operation successfully set to %d."), opNum);
			theApp.m_pDlg->m_pPhaseExecDialog->EchoStrAdd(msg);
			theApp.m_pDlg->m_pPhaseExecDialog->EditUpdate();
		}
	}

	this->m_cmbNextOp.GetLBText(this->m_cmbNextOp.GetCurSel(), str);
	opNum = _ttoi(str);
	if(opNum != nextOp){
		retSt = SetDynNextPhase(opNum);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error setting next operation."), _T("Error"), MB_OK);
			theApp.m_pDlg->m_pPhaseExecDialog->EchoStrAdd(_T("Error changing next operation."));
			return;
		}
		else{
			_stprintf_s(msg, _T("Next operation successfully set to %d."), opNum);
			theApp.m_pDlg->m_pPhaseExecDialog->EchoStrAdd(msg);
			theApp.m_pDlg->m_pPhaseExecDialog->EditUpdate();
		}
	}
	
	theApp.m_pDlg->m_pPhaseExecDialog->m_Start.EnableWindow(true);
	CDialog::OnCancel();
}
